import { getType } from '@/utils';

export default function(initialState: any) {
  const { role, menuData = [] } = initialState;
  const permissions: string[] = [];
  const generatePermissions = (menu: any): void => {
    if (getType(menu) === 'object') {
      permissions.push(menu.path);
      return;
    }
    if (getType(menu) === 'array') {
      menu.forEach((ele: any) => {
        permissions.push(ele.path);
        if (ele.hasOwnProperty('children')) {
          generatePermissions(ele.children);
        }
      });
    }
  };
  generatePermissions(menuData);

  return {
    admin: role === 'admin',
    permissions,
  };
}
